<template>
  <div class="markdown-preview">
     <ArticleList :articleInfos="articleData" :curPageNo="pageNo" :curTag="tag"  :maxItems="maxItems"/>
  </div>
</template>

<script>
import ArticleList from '~/components/ArticleList.vue';
export default {
  asyncData ({ app, params, query, env, error }) {
    let offset = 0;
    let tagParam = '';

    if (params.no) offset = (params.no - 1) * env.page.lineNumber;
    if (params.tag.toUpperCase() !== "ALL") tagParam ='&tag=' + params.tag;

    let apiurl = `${env.apiServer.baseUrl}/articles?offset=${offset}&limit=${env.page.lineNumber}${tagParam}`;
    return app.$axios.get(apiurl)
      .then((res) => {
        if (res.data.code === 0){
          return {
            tag: params.tag,
            pageNo: +params.no,
            articleData: res.data.data,
            maxItems: env.page.lineNumber,
          }
        } else {
          error({ statusCode: 404, message: res.data.message })
        }
      })
      .catch(function (e) {
        error({ statusCode: 404, message: e.message })
      });
  },
  components: {
    ArticleList
  }

}
</script>
